import java.sql.*;

public class DBTest {
    private static final String URL = "jdbc:mysql://localhost:3306/timerdb?useSSL=false&serverTimezone=UTC";
    private static final String USER = "root";
    private static final String PASSWORD = "123456";

    public static void main(String[] args) {
        try {
            // Load driver
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("MySQL driver loaded successfully!");

            // Test connection
            try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD)) {
                System.out.println("Database connection successful!");

                // Check if database exists
                try (Statement stmt = conn.createStatement()) {
                    // Check timerdb database
                    ResultSet rs = stmt.executeQuery("SHOW DATABASES LIKE 'timerdb'");
                    if (rs.next()) {
                        System.out.println("timerdb database exists!");
                        
                        // Check tasks table
                        rs = stmt.executeQuery("SHOW TABLES FROM timerdb LIKE 'tasks'");
                        if (rs.next()) {
                            System.out.println("tasks table exists!");
                        } else {
                            System.out.println("Creating tasks table...");
                            stmt.execute("CREATE TABLE IF NOT EXISTS tasks (" +
                                "id INT AUTO_INCREMENT PRIMARY KEY," +
                                "name VARCHAR(255) NOT NULL," +
                                "start_time DATETIME," +
                                "end_time DATETIME," +
                                "duration_seconds INT)");
                            System.out.println("tasks table created successfully!");
                        }
                    } else {
                        System.out.println("Creating timerdb database...");
                        stmt.execute("CREATE DATABASE IF NOT EXISTS timerdb DEFAULT CHARSET utf8mb4");
                        System.out.println("timerdb database created successfully!");
                        
                        // Create tasks table
                        stmt.execute("USE timerdb");
                        stmt.execute("CREATE TABLE IF NOT EXISTS tasks (" +
                            "id INT AUTO_INCREMENT PRIMARY KEY," +
                            "name VARCHAR(255) NOT NULL," +
                            "start_time DATETIME," +
                            "end_time DATETIME," +
                            "duration_seconds INT)");
                        System.out.println("tasks table created successfully!");
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
            e.printStackTrace();
        }
    }
} 